<?php

declare(strict_types=1);

namespace Tool\Database\MockType;

use Tool\Database\AbstractColumn;

/**
 * 时间类型.
 */
class Date extends AbstractColumn
{
    public function __construct(array &$column)
    {
        parent::__construct($column);

        $this->extArgs[] = date('Y-m-d 00:00:00', strtotime('-1 year'));
        $this->extArgs[] = date('Y-m-d 00:00:00');
    }

    /**
     * {@inheritDoc}
     */
    protected function mock()
    {
        if (2 === count($this->extArgs)) {
            return $this->format(mt_rand(strtotime($this->extArgs[0]), strtotime($this->extArgs[1])));
        }

        return $this->defaultValue();
    }

    protected function format(int $value): string
    {
        $format = [
            'date' => 'Y-m-d',
            'time' => 'H:i:s',
            'year' => 'Y',
            'timestamp' => 'Y-m-d H:i:s',
            'datetime' => 'Y-m-d H:i:s',
        ];

        return date($format[$this->getType()], $value);
    }
}
